package com.shiny.concurrency4java.atomic;

import com.shiny.concurrency4java.annoations.ThreadSafe;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

@ThreadSafe
@Slf4j
public class ReferenceFiledUpdaterExample {

    @Getter
    private volatile int count = 100;
    private static AtomicIntegerFieldUpdater<ReferenceFiledUpdaterExample> filedupdate =
            AtomicIntegerFieldUpdater.newUpdater(ReferenceFiledUpdaterExample.class,"count");

    private static ReferenceFiledUpdaterExample example = new ReferenceFiledUpdaterExample();

    public static void main(String[] args) throws Exception{
        if ( filedupdate.compareAndSet(example,100,120)) {
            log.info("update succ count: {}",example.getCount());

        } else {
            log.error("update failed count: {}",example.getCount());
        }
        return;
    }


}
